package com.mdp.workflow.ru.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.ru.entity.ProcinstTag;
import com.mdp.workflow.ru.service.ProcinstTagService;
import com.mdp.workflow.ru.vo.ProcinstTagVo;
import com.mdp.workflow.ru.service.ProcinstParamesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/**/ru/procinstTag")
@Api(tags={"会员标签登记表-操作接口"})
public class ProcinstTagController {
	
	static Logger logger =LoggerFactory.getLogger(ProcinstTagController.class);
	
	@Autowired
	private ProcinstTagService procinstTagService;

	@Autowired
	private ProcinstParamesService procinstParamesService;

	@ApiOperation( value = "会员标签登记表-查询列表",notes=" ")
	@ApiEntityParams(ProcinstTag.class)
	@ApiResponses({
		@ApiResponse(code = 200,response=ProcinstTag.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listProcinstTag(@ApiIgnore @RequestParam Map<String,Object> params){
		try {
			User user=LoginUtils.getCurrentUserInfo();
			QueryWrapper<ProcinstTag> qw = QueryTools.initQueryWrapper(ProcinstTag.class , params);
			IPage page=QueryTools.initPage(params);
			List<Map<String,Object>> datas = procinstTagService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());
		}catch (BizException e) {
			return Result.error(e);
		}catch (Exception e) {
			return Result.error(e);
		}
	}

	/**
	 * 批量打标签
	 * [
	 * 	{procDefId:'',procInstId:'',branchId:'',userid:'',tags:[
	 * 															{tagId:'',tagName:''}
	 * 														   ]
	 * 	}
	 * ]
	 * @param tagsVos
	 * @return
	 */
	@RequestMapping(value="/batchInsertOrDeleteTags",method=RequestMethod.POST)
	public Result batchDelProcinstTag(@RequestBody List<ProcinstTagVo> tagsVos) {

			if(tagsVos==null) {
				return Result.error("请上送参数列表");
			}else if(tagsVos.size()>10) {
				return Result.error("一次只能同时给十个流程打标签");
			}else {
				for (ProcinstTagVo tagsVo : tagsVos) {
					List<com.mdp.workflow.ru.entity.ProcinstTag> tags=tagsVo.getTags();
					String procInstId=tagsVo.getProcInstId();
					String procDefId=tagsVo.getProcDefId();
					String branchId=tagsVo.getBranchId();
					String userid=tagsVo.getUserid();
					String tagIds="";
					String tagNames="";
					if(tags!=null && tags.size()>0) {
						for (com.mdp.workflow.ru.entity.ProcinstTag tag : tags) {
							tagIds=tagIds+","+tag.getTagId();
							tagNames=tagNames+","+tag.getTagName();
						}
						tagIds=tagIds.substring(1);
						tagNames=tagNames.substring(1);
					}
					procinstParamesService.updateTagsByProDefIdAndProcInstId(branchId,procDefId,procInstId,tagIds,tagNames);
					procinstTagService.batchInsertOrDeleteTags(branchId,procDefId,procInstId,userid,tags);
				}
				return Result.ok();
			}
	}

}
